/*
Copyright 2011 Raymond Giorgi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package edu.pitt.cs3530.shareddatastructures;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import edu.pitt.cs3530.applicationcontrolling.ServerController;
import edu.pitt.cs3530.configuration.FrontEndConfiguration;
import edu.pitt.cs3530.objects.utilization.SimpleUtilization;

public class Utilization {
	private static Map<String, SimpleUtilization[]> utilizationMap = new HashMap<String, SimpleUtilization[]>();
	public static final String lock = new String();

	public static void addUtilization(String host, SimpleUtilization utilization) {
		if (utilizationMap.containsKey(host)) {
			SimpleUtilization[] utils = utilizationMap.get(host);
			for (int i = 1; i < utils.length; i++) {
				utils[i] = utils[i - 1];
			}
		} else {
			utilizationMap.put(host,
					new SimpleUtilization[FrontEndConfiguration.getInstance()
							.getUtilizationToKeep()]);
		}
		utilizationMap.get(host)[0] = utilization;
	}

	public static double getBlocked(String host) {
		if (utilizationMap.containsKey(host)) {
			SimpleUtilization[] utils = utilizationMap.get(host);
			double totalBlocked = 0.0;
			int noUtils = 0;
			for (int i = 0; i < utils.length; i++) {
				if (utils[i] != null) {
					noUtils++;
					totalBlocked += utils[i].getBlocked();
				}
			}
			return totalBlocked / noUtils;
		}
		return Double.MIN_VALUE;
	}

	public static double getIdle(String host) {
		if (utilizationMap.containsKey(host)) {
			SimpleUtilization[] utils = utilizationMap.get(host);
			double totalBlocked = 0.0;
			int noUtils = 0;
			for (int i = 0; i < utils.length; i++) {
				if (utils[i] != null) {
					noUtils++;
					totalBlocked += utils[i].getIdle();
				}
			}
			return totalBlocked / noUtils;
		}
		return Double.MIN_VALUE;
	}

	public static double getUsed(String host) {
		if (utilizationMap.containsKey(host)) {
			SimpleUtilization[] utils = utilizationMap.get(host);
			double totalBlocked = 0.0;
			int noUtils = 0;
			for (int i = 0; i < utils.length; i++) {
				if (utils[i] != null) {
					noUtils++;
					totalBlocked += utils[i].getUsed();
				}
			}
			return totalBlocked / noUtils;
		}
		return Double.MIN_VALUE;
	}

	public static double getClusterAverageUsed() {
		double retval = 0.0;
		int totalValues = 0;
		for (Entry<String, SimpleUtilization[]> entry : utilizationMap
				.entrySet()) {
			if (ServerController.getInstance().getDatabaseHostname() == null
					|| !entry.getKey().equals(
							ServerController.getInstance()
									.getDatabaseHostname())) {
				SimpleUtilization[] utils = entry.getValue();
				for (int i = 0; i < utils.length; i++) {
					if (utils[i] != null) {
						totalValues++;
						retval += utils[i].getUsed();
					}
				}
			}
		}
		return retval / totalValues;
	}

	public static void clearUtilization(String hostname) {
		utilizationMap.remove(hostname);
	}

	public static double getClusterAverageBlocked() {
		double retval = 0.0;
		int totalValues = 0;
		for (SimpleUtilization[] utils : utilizationMap.values()) {
			for (int i = 0; i < utils.length; i++) {
				if (utils[i] != null) {
					totalValues++;
					retval += utils[i].getBlocked();
				}
			}
		}
		return retval / totalValues;
	}

	public static double getClusterAverageIdle() {
		double retval = 0.0;
		int totalValues = 0;
		for (SimpleUtilization[] utils : utilizationMap.values()) {
			for (int i = 0; i < utils.length; i++) {
				if (utils[i] != null) {
					totalValues++;
					retval += utils[i].getIdle();
				}
			}
		}
		return retval / totalValues;
	}
}
